﻿using Microsoft.AspNetCore.Http;

namespace HZY.Core.ApiResultManage;

/// <summary>
/// 警告消息
/// </summary>
[Serializable]
public class MessageBox : Exception
{
    private R ApiResult { get; set; }

    public MessageBox(R apiResult) : base(string.Empty)
    {
        ApiResult = apiResult;
    }

    public MessageBox(string message) : base(message)
    {
        ApiResult = R.ErrorMessage(message);
    }

    public MessageBox(string message, object data) : base(message)
    {
        ApiResult = R.Error(message, data);
    }

    /// <summary>
    /// 获取 ApiResult
    /// </summary>
    /// <returns></returns>
    public R GetApiResult() => ApiResult;

    /// <summary>
    /// 创建消息
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public static MessageBox Show(string message) => new(message);

    /// <summary>
    /// 创建消息 输出消息和数据
    /// </summary>
    /// <param name="message"></param>
    /// <param name="data"></param>
    public static MessageBox Show(string message, object data) => new(message, data);

    /// <summary>
    /// 创建消息 支持传入 ApiResult 对象
    /// </summary>
    /// <param name="apiResult"></param>
    public static MessageBox Show(R apiResult) => new(apiResult);


    /// <summary>
    /// Create error message
    /// </summary>
    /// <param name="apiResult"></param>
    /// <returns></returns>
    public static MessageBox CreateMessage(R apiResult)
    {
        return new MessageBox(apiResult);
    }
}